#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)  
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

const int N = 1e5+9;
int _,n,a[2*N],b[2*N],len,x,over;
char ch[2*N];
bool flag;

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> _;
    while(_--){
        over = 0;
        cin >> ch+1 >> x;
        len = strlen(ch+1);
        rep(i,1,len) b[i] = ch[i] - '0' , a[i] = 1;
        rep(i,1,len){
            if(b[i]) continue;
            if(i-x > 0) a[i-x] = 0;
            if(i+x <= len) a[i+x] = 0;
        }
        rep(i,1,len){
            flag = false;
            if(!b[i]) continue;
            if(i-x > 0) flag |= a[i-x];
            if(i+x <= len) flag |= a[i+x];
            if(!flag){
                over = 1;
                break;
            }
        }
        if(over) cout << "-1\n";
        else{
            rep(i,1,len) cout << a[i];
            cout << "\n";
        }
    }
    return 0;
}